﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MyBlog2.Models;
using MyBlog2.Repository;
using MyBlog2.ViewModels;

namespace MyBlog2.Controllers
{
    public class HomeController : Controller
    {
        private readonly IArticleRepository _articleRepository;
        private readonly ITagRepository _tagRepository;
        private readonly IUserRepository _userRepository;
        private readonly IArticleClassifyRepository _classifyRepository;
        private readonly IFriendLinkRepository _friendLinkRepository;
        private readonly ICommentRepository _commentRepository;

        public HomeController(IArticleRepository articleRepository, ITagRepository tagRepository,IUserRepository userRepository, IArticleClassifyRepository classifyRepository, IFriendLinkRepository friendLinkRepository, ICommentRepository commentRepository)
        {
            this._articleRepository = articleRepository;
            this._tagRepository = tagRepository;
            this._userRepository = userRepository;
            this._classifyRepository = classifyRepository;
            this._friendLinkRepository = friendLinkRepository;
            this._commentRepository = commentRepository;
        }


        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="tagTopCount"></param>
        /// <returns></returns>
        [Route("")]
        [Route("Home")]
        [Route("Home/Index")]
        [ResponseCache(Duration = 600)]     //设置浏览器端缓存（加入服务器缓存中间件后变为服务器缓存）
        public IActionResult Index(int pageIndex = 1, int pageSize = 8,int tagTopCount=10)
        {
            var arts = _articleRepository.GetArticleList(pageIndex, pageSize, out int totalPageCount);
            var tags = _tagRepository.GetTagList(tagTopCount);
            var usr = _userRepository.GetUserInfo();
            var fls = _friendLinkRepository.GetFriendLinks();
            if (arts != null  && usr != null)
            {
                var indexVM = new IndexViewModel
                {
                    Articles = arts,
                    Tags = tags,
                    User = usr,
                    FriendLinks = fls,
                    totalPageCount = totalPageCount,
                    pageIndex = pageIndex,
                };
                return View(indexVM);
            }
            else
            {
                return NotFound();
            }
        }


        /// <summary>
        /// 文章搜索
        /// </summary>
        /// <param name="keywords"></param>
        /// <returns></returns>
        [Route("Home/Sreach")]
        public IActionResult Sreach(string keywords)
        {
            if (string.IsNullOrEmpty(keywords))
            {
                return BadRequest("请输入搜索关键字！");
            }
            var arts = _articleRepository.SearchArticles(a=>a.Title.Contains(keywords) || a.Contents.Contains(keywords));
            if (arts == null)
            {
                return BadRequest("没有搜索到相关文章！");
            }
            return View(arts);
        }


        /// <summary>
        /// 文章详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Home/Detial")]
        public IActionResult Detial(int id)
        {
            if (HttpContext.User.Identity.IsAuthenticated)  //管理员已登录
            {
                ViewBag.usr = _userRepository.GetUserInfo();
            }
            else
            {
                ViewBag.usr = null;
            }

            var art = _articleRepository.GetOneArticle(id);
            var com = _commentRepository.GetCommentList(id);
            if (art != null)
            {
                var detialVM = new DetialViewModel
                {
                    Article = art,
                    Comments = com,
                };
                _articleRepository.UpdateViews(art);
                return View(detialVM);

            }
            else
            {
                return NotFound();
            }
            
        }


        /// <summary>
        /// 分类页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Home/Classify")]
        public IActionResult Classify(int id=-1)
        {
            var a = _articleRepository.GetArticleListByClassifyId(id);
            var c = _classifyRepository.GetArticleClassifieList("文章数量排序");
            if (c != null || (a != null && c != null))
            {
                var classifyVM = new ClassifyViewModel
                {
                    Articles = a,
                    Classifies = c,
                };
                return View(classifyVM);
            }
            else
            {
                return NotFound();
            }
            
        }


        /// <summary>
        /// 标签页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Home/Tag")]
        public IActionResult Tag(int id=-1)
        {
            var arts = _articleRepository.GetArticleListByTagId(id);
            var tags = _tagRepository.GetTagList("文章数量排序");
            if (tags != null || (tags != null && arts != null))
            {
                var tagVM = new TagViewModel
                {
                    Articles = arts,
                    Tags = tags,
                };
                return View(tagVM);
            }
            else
            {
                return NotFound();
            }
        }


        /// <summary>
        /// 归档页
        /// </summary>
        /// <returns></returns>
        [Route("Home/Archive")]
        //[ResponseCache(Duration = 600)]
        public IActionResult Archive()
        {
            ViewBag.totalCount = _articleRepository.GetArticleCount(a=>true);
            var archiveDic = _articleRepository.ArticleArchive(_articleRepository.GetYears());
            return View(archiveDic);
        }


        /// <summary>
        /// 关于页
        /// </summary>
        /// <returns></returns>
        [Route("Home/About")]
        [ResponseCache(Duration = 600)]
        public IActionResult About()
        {
            return View();
        }
    }
}
